/**
 * Created by gongxun on 17/1/13.
 */

var GsbdxTableCardLayer = cc.Layer.extend({
    mBatchNode : null,
    mBatchNode2 : null,
    mGsbdxTableDiceLayer : null,
    //牌墙
    mCards : null,
    //牌墙状态 0:20 1:10
    mCardWellType : null,
    //洗牌动画
    mSpriteXipai : null,
    //放每个玩家面前的牌
    mSeatCards : null,
    ctor : function(){
        this._super();

        this.mBatchNode = new cc.Node();
        this.mBatchNode.setContentSize(cc.size(mo.gameSize.x, mo.gameSize.y));
        this.mBatchNode.setAnchorPoint(cc.p(0.5, 0.5));
        this.mBatchNode.setPosition(cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2));
        this.addChild(this.mBatchNode);

        this.mBatchNode2 = new cc.Node();
        this.mBatchNode2.setContentSize(cc.size(mo.gameSize.x, mo.gameSize.y));
        this.mBatchNode2.setAnchorPoint(cc.p(0.5, 0.5));
        this.mBatchNode2.setPosition(cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2));
        this.addChild(this.mBatchNode2);

        this.mGsbdxTableDiceLayer = new GsbdxTableDiceLayer();
        this.addChild(this.mGsbdxTableDiceLayer);
        this.mGsbdxTableDiceLayer.setVisible(false);

        this.mSpriteXipai = new cc.Sprite("res/ttz_light_xipai.png");
        this.mSpriteXipai.setPosition(cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2 - 10));
        this.mSpriteXipai.setVisible(false);
        this.addChild(this.mSpriteXipai);

        //牌
        this.mCards = [];
        for(var i = 0; i < 20; i++){
            var card = new GsbdxCard();
            card.setVisible(false);
            this.mCards.push(card);
            if (i < 10){
                this.mBatchNode.addChild(card);
            }else{
                this.mBatchNode2.addChild(card);
            }
        }

        //显示默认牌墙
        this.setDefaultCardWell();
    },

    //显示牌墙
    showCardWell : function(cardNums, isAnim){
        this.mCardWellType = mo.gsbdxTableStatus.getCardWellType();
        this.mBatchNode.setPosition(cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2));
        this.mBatchNode2.setPosition(cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2));
        this.mBatchNode2.setScale(1);
        var cardWellPos = cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2);

        var endPoss = [];
        for (var i = 0; i < 20; i++){
            endPoss.push(mo.gsbdxCardHelper.getHideCardPos(i, cardWellPos));
        }

        this.mBatchNode2.stopAllActions();
        this.mBatchNode2.setPosition(cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2));
        this.mBatchNode2.setScale(1);

        this.mSpriteXipai.stopAllActions();
        this.mSpriteXipai.setVisible(false);
        this.mSpriteXipai.setOpacity(255);

        if (!isAnim){
            if (this.mCardWellType == 0){
                for (var i = 0; i < 20; i++){
                    var card = this.mCards[i];
                    card.stopAllActions();
                    card.setCardNum(0);
                    card.setCardKind(0, 2);
                    card.setPosition(endPoss[i]);
                    card.setVisible(true);
                }
            }else{
                var index = 0;
                for (var i = 0; i < 20; i++){
                    var card = this.mCards[i];
                    card.stopAllActions();
                    if ( i % 10 > 4){
                        card.setPosition(mo.gsbdxCardHelper.getShowCardPos(index, cardWellPos));
                        card.setCardNum(cardNums[index]);
                        card.setCardKind(0, 2);
                        card.setVisible(true);
                        index++;
                    }else{
                        card.setPosition(mo.gsbdxCardHelper.getHideCardPos(i, cardWellPos));
                        card.setCardNum(0);
                        card.setCardKind(0, 2);
                        card.setVisible(true);
                    }
                }
            }
            return;
        }

        if (this.mCardWellType == 0){
            this.mBatchNode2.setPosition(cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2 - 80));

            this.mSpriteXipai.setVisible(true);

            this.mSpriteXipai.runAction(cc.sequence(
                cc.fadeOut(0.5),
                cc.hide()
            ));

            for (var i = 0; i < 20; i++){
                var card = this.mCards[i];
                card.stopAllActions();
                card.setCardNum(0);
                card.setCardKind(0, 2);
                card.setPosition(endPoss[i]);
                card.setVisible(true);
            }

            this.mBatchNode2.runAction(cc.sequence(
                cc.delayTime(0.5),
                cc.spawn(
                    cc.moveTo(0.2, cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2 - 80)),
                    cc.scaleTo(0.2, 1.1)
                ),
                cc.spawn(
                    cc.moveTo(0.3, cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2)),
                    cc.scaleTo(0.3, 1.3)
                ),
                cc.callFunc(function(){
                    mo.soundHelper.playGsbdxShuffleCard();
                }),
                cc.scaleTo(0.2, 1)
            ));
        }else{
            this.mSpriteXipai.setVisible(true);

            this.mSpriteXipai.runAction(cc.sequence(
                cc.fadeOut(0.5),
                cc.hide()
            ));

            var showCards = [];
            for (var i = 0; i < 20; i++){
                var card = this.mCards[i];
                if ( i % 10 > 4){
                    showCards.push(card);
                    card.setCardNum(cardNums[showCards.length - 1]);
                    card.setCardKind(0, 2);
                    card.setVisible(true);
                }else{
                    card.setPosition(mo.gsbdxCardHelper.getHideCardPos(i, cardWellPos));
                    card.setCardNum(0);
                    card.setCardKind(0, 2);
                    card.setVisible(true);
                }
            }
            for (var i = 0; i < showCards.length; i++){
                showCards[i].stopAllActions();
                var toPos = mo.gsbdxCardHelper.getShowCardPos(i, cardWellPos);
                var toScale = mo.gsbdxCardHelper.cardWellScale;
                showCards[i].runAction(cc.sequence(
                    cc.spawn(
                        cc.moveTo(0.3, toPos),
                        cc.scaleTo(0.3, toScale)
                    )
                ));
            }
        }
    },

    //发牌
    setFapaiAnim : function(dices, toViewSeat, isAnim){
        this.mCardWellType = mo.gsbdxTableStatus.getCardWellType();
        this.mSeatCards = [[], [], [], [], []];

        //摇骰子
        var startIndex = this.mCardWellType == 0 ? 9 : 4;

        if (isAnim){
            var delayTime = this.mGsbdxTableDiceLayer.showDiceAnim(dices, toViewSeat);
            for (var i = 0; i < 5; i++){
                var seatView = (i + toViewSeat) % 5;
                var index1 = startIndex - i;
                var index2 = startIndex + 10 - i;
                var card1 = this.mCards[index1];
                card1.setVisible(true);
                var card2 = this.mCards[index2];
                card2.setVisible(true);

                this.mSeatCards[seatView].push(card1);
                this.mSeatCards[seatView].push(card2);

                card1.stopAllActions();
                card2.stopAllActions();

                card1.runAction(cc.sequence(
                    cc.delayTime(delayTime + i * 0.1),
                    cc.callFunc(function(){
                       mo.soundHelper.playGsbdxSendCard();
                    }),
                    cc.spawn(
                        cc.moveTo(0.4, mo.gsbdxCardHelper.getInhandCardPos(seatView, 0)),
                        cc.scaleTo(0.4, seatView == 0 ? mo.gsbdxCardHelper.inhandMyScale : mo.gsbdxCardHelper.inhandScale)
                    )
                ));

                card2.runAction(cc.sequence(
                    cc.delayTime(delayTime + i * 0.1),
                    cc.spawn(
                        cc.moveTo(0.4, mo.gsbdxCardHelper.getInhandCardPos(seatView, 1)),
                        cc.scaleTo(0.4, seatView == 0 ? mo.gsbdxCardHelper.inhandMyScale : mo.gsbdxCardHelper.inhandScale)
                    )
                ));
            }
        }else{
            this.mGsbdxTableDiceLayer.setVisible(false);
            for (var i = 0; i < 5; i++){
                var seatView = (i + toViewSeat) % 5;
                var index1 = startIndex - i;
                var index2 = startIndex + 10 - i;
                var card1 = this.mCards[index1];
                var card2 = this.mCards[index2];

                this.mSeatCards[seatView].push(card1);
                this.mSeatCards[seatView].push(card2);

                card1.stopAllActions();
                card1.setVisible(true);
                card1.setPosition(mo.gsbdxCardHelper.getInhandCardPos(seatView, 0));
                card1.setCardNum(0);
                card1.setCardKind(seatView, 1);

                card2.stopAllActions();
                card2.setVisible(true);
                card2.setPosition(mo.gsbdxCardHelper.getInhandCardPos(seatView, 1));
                card2.setCardNum(0);
                card2.setCardKind(seatView, 1);
            }
        }
    },

    //开牌
    setKaipaiSeat : function(seat, cardNums, isAnim){
        this.mGsbdxTableDiceLayer.setVisible(false);
        var card1 = this.mSeatCards[seat][0];
        var card2 = this.mSeatCards[seat][1];

        card1.stopAllActions();
        card1.setVisible(true);
        card1.setPosition(mo.gsbdxCardHelper.getInhandCardPos(seat, 0));
        card1.setCardNum(0);
        card1.setCardKind(seat, 1);

        card2.stopAllActions();
        card2.setVisible(true);
        card2.setPosition(mo.gsbdxCardHelper.getInhandCardPos(seat, 1));
        card2.setCardNum(0);
        card2.setCardKind(seat, 1);

        if (isAnim){
            var animLen = 8;
            var spriteFrames = [];
            spriteFrames.push(cc.spriteFrameCache.getSpriteFrame("ttz_cardHide.png"));
            for (var i = 0; i <= 5; i++){
                var frame = cc.spriteFrameCache.getSpriteFrame("ttz_cardAnim" + i + ".png");
                if (frame == null){
                    break;
                }
                spriteFrames.push(frame);
            }
            spriteFrames.push(cc.spriteFrameCache.getSpriteFrame("ttz_cardOpen.png"));

            for (var i = 0; i < 2; i++){
                var card = i == 0 ? card1 : card2;

                var animation = new cc.Animation(spriteFrames);
                animation.setDelayPerUnit(0.05);
                card.setTag(cardNums[i]);
                card.runAction(cc.sequence(
                    cc.spawn(
                        cc.animate(animation),
                        cc.sequence(
                            cc.moveBy(0.2, cc.p(0, 10)),
                            cc.moveBy(0.2, cc.p(0, -10))
                        )
                    ),
                    cc.callFunc(function(){
                        mo.soundHelper.playGsbdxShuffleCard();
                        this.setCardNum(this.getTag());
                        this.setCardKind(seat, 1);
                    }, card)
                ));
            }
        }else{
            for (var i = 0; i < 2; i++){
                this.mSeatCards[seat][i].setCardNum(cardNums[i]);
                this.mSeatCards[seat][i].setCardKind(seat, 1);
            }
        }
    },

    //显示默认牌墙
    setDefaultCardWell : function(){
        this.mBatchNode2.stopAllActions();
        this.mBatchNode2.setPosition(cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2 - 80));

        var cardWellPos = cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2);

        var endPoss = [];
        for (var i = 0; i < 20; i++){
            endPoss.push(mo.gsbdxCardHelper.getHideCardPos(i, cardWellPos));
        }

        for (var i = 0; i < 20; i++){
            var card = this.mCards[i];
            card.stopAllActions();
            card.setCardNum(0);
            card.setCardKind(0, 2);
            card.setPosition(endPoss[i]);
            card.setVisible(true);
        }
    }
});